--[[
title: 文件系统
description: 文件系统相关函数
]]

local __application__ = _G.__application__;

-- 导入C++函数
commonlib.GetFullPath = __application__.GetFullPath;
commonlib.GetFileText = __application__.GetFileText;
commonlib.FormatPath = __application__.FormatPath;
commonlib.IsAbsolutePath = __application__.IsAbsolutePath;
commonlib.IsDirectory = __application__.IsDirectory;
commonlib.IsRegularFile = __application__.IsRegularFile;
commonlib.MakeDirectory = __application__.MakeDirectory;
commonlib.DeleteDirectory = __application__.DeleteDirectory;
commonlib.GetRootDirectory = __application__.GetRootDirectory;
commonlib.GetStorageDirectory = __application__.GetStorageDirectory;

-- 获取文件目录
function commonlib.GetDirectory(filepath)
    local directory = string.gsub(filepath or "", "([^\\/]*)$", "");
    return directory;
end

-- 获取文件名
function commonlib.GetFileName(filepath)
    local filename = string.match(filepath or "", "([^\\/]*)$");
    return filename;
end

-- 判断文件是否存在
function commonlib.IsExistFile(filepath)
    local file = io.open(filepath, "rb");
    if (not file) then return false end
    file:close();
    return true;
end

-- 读取文件
function commonlib.ReadFile(filepath)
    local file = io.open(filepath, "rb");
    if (not file) then return nil end
    local text = file:read("*a");
    file:close();
    return text;
end

-- 写入文件
function commonlib.WriteFile(filepath, text)
    local file = io.open(filepath, "wb");
    if (not file) then return false end
    file:write(text);
    file:close();
    return true;
end
